import request from '@/utils/request'

// 机构管理相关API
export function getOrgList(params) {
  const query = new URLSearchParams()
  if (params && params.org_id !== undefined) query.append('org_id', params.org_id)
  if (params && params.org_level !== undefined) query.append('org_level', params.org_level)
  const url = `/management/system/org/list${query.toString() ? `?${query.toString()}` : ''}`
  return request.get(url)
}

export function addOrg(data) {
  return request.post('/management/system/org/add', data)
}

export function updateOrg(data) {
  return request.post('/management/system/org/update', data)
}

export function deleteOrg(params) {
  const query = new URLSearchParams()
  if (params && params.org_id !== undefined) query.append('org_id', params.org_id)
  if (params && params.org_level !== undefined) query.append('org_level', params.org_level)
  const url = `/management/system/org/delete${query.toString() ? `?${query.toString()}` : ''}`
  return request.delete(url)
}

export function updateOrgStatus(params) {
  const query = new URLSearchParams()
  if (params && params.org_id !== undefined) query.append('org_id', params.org_id)
  if (params && params.status !== undefined) query.append('status', params.status)
  const url = `/management/system/org/status${query.toString() ? `?${query.toString()}` : ''}`
  return request.put(url, null)
}

// 获取机构详情
export function getOrgDetail(params) {
  const query = new URLSearchParams()
  if (params && params.org_id !== undefined) query.append('org_id', params.org_id)
  const url = `/management/system/org/detail${query.toString() ? `?${query.toString()}` : ''}`
  return request.get(url)
}

// 获取机构树形结构
export function getOrgTree(params) {
  const query = new URLSearchParams()
  if (params && params.org_id !== undefined) query.append('org_id', params.org_id)
  const url = `/management/system/org/tree${query.toString() ? `?${query.toString()}` : ''}`
  return request.get(url)
}

// 为机构生成并添加一个用户（userId 可选，不传则后端生成）
export function addUserToOrg(params) {
  const query = new URLSearchParams()
  if (params && params.userId !== undefined && params.userId !== '') query.append('userId', params.userId)
  if (params && params.orgId !== undefined) query.append('orgId', params.orgId)
  const url = `/management/system/org/add_user${query.toString() ? `?${query.toString()}` : ''}`
  return request.post(url)
}

// 将账户移出该机构（批量）
// 后端文档：POST /system/org/remove_user  Body: ["uid1","uid2",...]
export function removeUsersFromOrg(uidList) {
  const url = `/management/system/org/remove_user`
  const body = Array.isArray(uidList) ? uidList : []
  return request.post(url, body)
}

// 生成机构邀请码
// GET /management/system/org/invitation_code
// params: { orgId, checkCode } headers: { code_token? }
export function getOrgInvitationCode(params = {}, headers = {}) {
  const query = new URLSearchParams()
  if (params.orgId !== undefined && params.orgId !== null && params.orgId !== '') query.append('orgId', params.orgId)
  const url = `/management/system/org/invitation_code${query.toString() ? `?${query.toString()}` : ''}`
  return request.get(url, { headers })
}

// 批量添加人员进入某机构
export function batchAddUsersToOrg({ file, org_id }) {
  const form = new FormData()
  if (file) form.append('users', file)
  if (org_id !== undefined && org_id !== null && org_id !== '') form.append('org_id', org_id)
  const url = `/management/system/org/batch_add_user`
  return request.post(url, form)
}
